package SingleMachineArrangeImpl;

/*2.1.1 加权总完工时间问题（无就绪时间）*/
//加权最短完工时间优先
public class WSPT extends SingleMachineArrange{

    @Override
    public void solve() {
        if (process == null || weight == null) {
            process = new int[]{12, 4, 7, 11, 6};
            weight = new int[]{4, 2, 5, 5, 6};
        }
        int n = process.length;
        final int COMPLETE = -1;
        double[] w_p = new double[n];
        System.out.print("[");
        for (int j = 0; j < n; j++) {
            w_p[j] = 1.0 * weight[j] / process[j];
            System.out.printf("%f, ", w_p[j]);
        }
        System.out.println("]");
        //当前工件加工用时
        int t;
        //总加工时间
        int tt = 0;
        //加权总加工时间
        int wtt = 0;
        for (int i = 0; i < n; i++) {
            double mwp = Double.MIN_VALUE;
            int wp = -1;
            //优先加工加权完工时间最大的工件
            for (int j = 0; j < n; j++) {
                if (process[j] == COMPLETE) continue;
                if (mwp < w_p[j]) {
                    mwp = w_p[j];
                    wp = j;
                }
            }
            t = process[wp];
            process[wp] = COMPLETE;
            tt += t;
            wtt += weight[wp] * tt;
            System.out.printf("t = %d, tt = %d, wtt = %d, wp = %d\n", t, tt, wtt, (wp + 1));
        }
        System.out.println("wtt = " + wtt);
    }


}
